package com.lipeng.controller;



import com.lipeng.bean.Photo;
;
import com.lipeng.service.PhotoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by dllo on 18/3/8.
 */
@Controller
public class PhotoController {
    @Resource
    private PhotoService photoService;

    @RequestMapping(value = "photolist")
    public String photoList(){
        return "photolist";
    }

    @RequestMapping(value = "photosearch")
    public String photoSearch(){
        return "photosearch";
    }

    @ResponseBody
    @RequestMapping(value = "allphoto")
    public List<Photo> findAllPhoto(){
        return photoService.findAllPhoto();
    }

    @ResponseBody
    @RequestMapping(value = "searchphotoname")
    public List<Photo> selectPhotoByName(@RequestParam("pname") String pname){
        return photoService.selectPhotoByName(pname);
    }


    @RequestMapping(value = "delphoto")
    public String deletePhotoById(HttpServletRequest request, @RequestParam("pid")Integer pid){
        int result = photoService.deletePhotoById(pid);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:photolist";
    }
}
